package com.shenhaofeng.app.loader.list;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.ViewGroup;

import com.shenhaofeng.app.loader.list.listener.OnBindDataListener;


/**
 * 快捷加载数据
 *
 * @param <R> 刷新View
 * @param <L> 数据View
 */
public class Loader<R extends ViewGroup, L extends ViewGroup, D> {


    private Component<D> mComponent;

    private boolean isAttached = false;


    /**
     * @param component
     */
    public Loader(@NonNull Component<D> component) {
        this.mComponent = component;
    }


    /**
     * 附加到可视化窗口
     *
     * @param refreshLayout
     * @param dataLayout
     * @param mDataSource
     * @param listener
     */
    public void attach(R refreshLayout, L dataLayout, DataSource<D> mDataSource, OnBindDataListener<D> listener) {
        isAttached = true;
        mComponent.attach(refreshLayout, dataLayout, mDataSource, listener);
    }


    /**
     * 启动初始化，在ui完成准备后执行
     */
    public void refresh() {
        if (!isAttached) {
            throw new IllegalStateException("必须附加到UI");
        }
        mComponent.refresh();
    }




    public void load() {
        if (!isAttached) {
            throw new IllegalStateException("必须附加到UI");
        }
    }



}

